<template>
  <view>
    <view class="goods-list-container">
      <view @click="gotoGoodsDetail(goods)" v-for="(goods,i) in goodsList" :key="i">
        <my-goods :goods="goods" ></my-goods>
      </view>

    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        total: 0,
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        //节流阀
        isLoading:false,
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      }
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || ''
      this.queryObj.query = options.query || ''
      this.getGoodsList()
    },
    onPullDownRefresh(){
      this.total=0;
      this.goodsList=[]
      this.isLoading=false,
      this.queryObj.pagenum=1
      this.getGoodsList(()=>{
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom(){
      if(this.queryObj.pagesize*this.queryObj.pagenum>=this.total) return uni.$showMsg("数据加载完毕")
      if(this.isLoading) return
      this.queryObj.pagenum+=1
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        //开启节流阀
        this.isLoading=true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        //关闭节流阀
        this.isLoading=false
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoGoodsDetail(goods){
        console.log("1111")
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id="+goods.goods_id
        })
      }

    }
    
    

  }
</script>

<style lang="scss">
.goods-list-container{
  
}
</style>